package itpenguin.common;

import itpenguin.entities.Article;
import itpenguin.entities.ImageSrc;
import itpenguin.entities.Series;

import java.io.IOException;
import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class Util {

	public static String getUriBase(HttpServletRequest request)
	{
		StringBuffer url = request.getRequestURL();
		int idx = url.lastIndexOf("/");
		return url.substring(0, idx);
	}
	
	public static String[] GetKey(HttpServletRequest req) {
		String pathinfo = req.getPathInfo();
		if (pathinfo == null)
		{
			return null;
		}
		
		String[] uri = req.getPathInfo().split("/");
		if (uri.length == 2)
		{
			return new String[]{uri[1]};
		} else if (uri.length == 3) {
			return new String[]{uri[1], uri[2]};
		} else {
			return null;
		}
		
	}
	
	public static List<ImageSrc> GetImageMapList(String articleId, PersistenceManager pm) {
		Query query = pm.newQuery("select from itpenguin.ImageMap "
				+ "where articleId == articleIdParam "
				+ "parameters String articleIdParam ");
		return (List<ImageSrc>) query.execute(articleId);
	}
	public static List<ImageSrc> GetImageMapList(String articleId) {
		
		PersistenceManager pm = null;
		try {
			pm = PMFactory.getInstance().getPersistenceManager();
			List<ImageSrc> list = GetImageMapList(articleId, pm);
			pm.detachCopyAll(list);
			return list;
		} finally {
			pm.close();
		}
	}
	
	public static List<Article> GetSeriesList(String seriesId, PersistenceManager pm) {
		Query query = pm.newQuery("select from itpenguin.entities.Article "
				+ "where parentId == parentIdParam "
				+ "parameters String parentIdParam "
				+" order by published");  
		return (List<Article>) query.execute(seriesId);
	}
	public static List<Article> GetSeriesList(String seriesId) {
		
		PersistenceManager pm = null;
		try {
			pm = PMFactory.getInstance().getPersistenceManager();
			List<Article> list = GetSeriesList(seriesId, pm);
			pm.detachCopyAll(list);
			return list;
		} finally {
			pm.close();
		}
	}
	
	public static List<Series> GetChildSheries(String seriesId) {
		PersistenceManager pm = PMFactory.getInstance().getPersistenceManager();
		Query query = pm.newQuery("select from itpenguin.entities.Series "
				+ "where parentId == parentIdParam "
				+ "parameters String parentIdParam "
				+" order by published");  
		return (List<Series>) query.execute(seriesId);
	}
	
	public static Article GetArticle(String key, PersistenceManager pm) {
		Key k = KeyFactory.createKey(Article.class.getSimpleName(), key); 
		Article article = null;
		
		try {
			article = pm.getObjectById(Article.class, k);
		} catch (JDOObjectNotFoundException e) {
			return null;
		}
		return article;
	}
	
	public static Article GetArticle(String key) throws IOException {
		return GetArticle(key, false);
	}
	
	public static Article GetArticle(String key, boolean readImageMap)
			throws IOException {
		PersistenceManager pm = null;
		try {
			pm = PMFactory.getInstance().getPersistenceManager();
			Article a = GetArticle(key, pm);
			if (readImageMap) {
				a.getImageMap();
			}
			return pm.detachCopy(a);
		} finally {
			pm.close();
		}
	}
	
	public static Series GetSeries(String key, PersistenceManager pm) {
		Key k = KeyFactory.createKey(Series.class.getSimpleName(), key); 
		Series series = null;
		
		try {
			series = pm.getObjectById(Series.class, k);
		} catch (JDOObjectNotFoundException e) {
			return null;
		}
		return series;
	}
	
	public static Series GetSeries(String key)
			throws IOException {
		PersistenceManager pm = null;
		try {
			pm = PMFactory.getInstance().getPersistenceManager();
			Series s = GetSeries(key, pm);
			pm.detachCopy(s);
			return s;
		} finally {
			pm.close();
		}
	}
	
}
